/**
 * Created by wwh on 2017/7/4.
 */
/**
 * 时间格式取年月日
 * @param time
 * @returns {*}
 */
function getYearMonthDate(time) {
    console.log(time);
    if(time!=null) {
        var YMD = time.split(" ")[0];  // 年月日
        var HMS = time.split(" ")[1];  // 时分秒
        return YMD;
    }
    return null;
}

layui.config({
    base: '/static/js/layuiModulesEX/'
});
layui.use(['paging', 'form', 'layer', 'element', 'laytpl','laydate','tab'], function () {
    var $ = layui.jquery,
        paging = layui.paging(),
        layerTips = parent.layer === undefined ? layui.layer : parent.layer, //获取父窗口的layer对象
        layer = layui.layer, //获取当前窗口的layer对象
        form = layui.form(),
        laytpl = layui.laytpl,
        laydate = layui.laydate,
        element = layui.element();

    var vm = new Vue({
        el: '#Vue',
        data: {
            show: true
            , user: {
                id: userid
            }
            , employmentInfo: {
                userid: userid
            }
            , orgInfo: {
                id: ''
            },
            studentInfo:{}

        },
        methods: {
            /**
             * 添加按钮
             */
            add: function () {
                console.log(vm.employmentInfo);
                vm.show = false;
                vm.employmentInfo={valid:1,userid:userid};
            },
            /**
             * 保存按钮
             */
            save: function () {
                var url = "/employmentinfo/add";
                // console.log(vm.employmentInfo);
                //绑定select下拉框有问题，故采取以下方法（待解决）
                vm.employmentInfo.employerid = $("#org option:selected").val();
                vm.employmentInfo.signdate = $("#signdate").val();

                axios.post(url, vm.employmentInfo)
                    .then(function (response) {
                        layer.msg(response.data.message, {icon:6,time: 1000}, function () {
                            if (response.data.success) {
                                vm.reload();
                            }
                        });
                    })
                    .catch(function (response) {
                        console.log(response);
                        layer.msg("保存失败", {icon: 2, time: 1000});
                    });
            },
            /**
             * 从添加就业信息页面返回主页面按钮
             */
            reload: function () {
                window.location.reload();//实现页面刷新
                $('#content input[type!=button]').attr("readOnly",true).addClass("disabled");
                $('#content textarea').attr("readOnly", true).addClass("disabled");

            },
            /**
             * 获取信息
             * @param userId
             */
            getInfo: function (userid) {
                // 清空脏数据，vm.employment仅包含用户id
                vm.employmentInfo={userid:userid};
                // console.log(vm.employmentInfo);
                $.ajax({
                    type: "POST",
                    url: "/employmentinfo/findByCondition",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(vm.employmentInfo),
                    dataType: "json",
                    success: function (Response) {
                        console.log(Response.data);
                        for (var i = 0,j=0,k=0; i < Response.data.length; i++) {
                            // console.log(Response.data[i].employmentInfo);
                            if(Response.data[i].employmentInfo.valid=="1"){
                                employmentInformation_effective[j++]=Response.data[i];
                            }
                            else{
                                employmentInformation_invalid[k++]=Response.data[i];
                            }
                        }
                        // console.log(employmentInformation_effective);
                        // console.log(employmentInformation_invalid);
                        //渲染就业信息列表
                        var data = {
                            "title": "就业信息列表",
                            "list_effective": employmentInformation_effective,
                            "list_invalid":employmentInformation_invalid
                        };
                        if (data.list_effective.length == 0&&data.list_invalid.length==0) {
                            layer.msg("您尚无就业信息记录，请点击添加", {icon: 6, time: 1500});
                        }
                        var getTpl = document.getElementById("demo").innerHTML;
                        laytpl(getTpl).render(data.list_effective, function (html) {
                            document.getElementById("view_effective").innerHTML = html;
                        });
                        laytpl(getTpl).render(data.list_invalid, function (html) {
                            document.getElementById("view_invalid").innerHTML = html;
                        });
                    },
                    error: function (msg) {
                        console.log("失败");
                        console.log(msg);
                    }
                });
            }
        }
    })

    // 首次获得信息
    vm.getInfo(userid);

    //渲染公司企业下拉框
    $.ajax({
        type: "POST",
        url: "/orginfo/findByCondition/",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({state:3}),   // 只可选择已通过审核的用人单位
        dataType: "json",
        success: function (Response) {
            for (var i = 0; i < Response.data.length; i++) {
                company[i] = Response.data[i];
            }
            var html = '';
            for (var i = 0; i < company.length;i++) {
                html += '<option value=' + company[i].id + ' >' + company[i].fullname + '</option>';
            }
            $(org).append(html);
            form.render('select');
            // console.log(company);
        },
        error: function (msg) {
            console.log("失败");
            console.log(msg);
        }
    });

    // 监听下拉框为必选项与提交
    form.on('submit(save)',function () {
        console.log($('#org').val());
        if($('#org').val()==null){
            layer.msg('请选择单位！',{icon:0,time:1000});
        }
        else{
            vm.save();
        }
    });
    // 监听添加用人单位按钮
    // var  origin = "/student/employment";
    // form.on('submit(addCompany)',function () {
        // $.ajax({
        //     type:"POST",
        //     data:"test="+a,
        //     url:"/student/addCompany-employmentInfo"
        //     ,success:function (res) {
        //         // console.log("return:  "+res);
        //         console.log("success");
        //     },error:function (e) {
        //         console.log(e);
        //     }
        // });
        // location.href='/student/addCompany-employmentInfo?test=' + origin;
        // location.href="/student/orgInfo?operateType=1&origin='/student/Employment'";
    // });
});
